<template>
  <div class="goods-comment" v-if="conditions">
    <div class="head">
      <div class="data">
        <p><span>{{conditions.evaluateCount}}</span><span>人购买</span></p>
        <p><span>{{conditions.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" :class="{active:currentIndex===index}" @click="changeCondition(index)" v-for='(item, index) in conditions.tags' :key='index' >{{item.title}}（{{item.tagCount}}）</a>
        </div>
      </div>
    </div>
     <div class="sort">
      <span>排序：</span>
      <a @click='changeSort(null)' href="javascript:;" :class='{active: reqParams.sortField===null}'>默认</a>
      <a @click='changeSort("createTime")' href="javascript:;" :class='{active: reqParams.sortField==="createTime"}'>最新</a>
      <a @click='changeSort("praiseCount")' href="javascript:;" :class='{active: reqParams.sortField==="praiseCount"}'>最热</a>
    </div>

    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar">
          <span>{{formatNickName(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i v-for="i in 5 - item.score" :key="i" class="iconfont icon-wjx02"></i>
            <span class="attr">{{formatSpec(item.orderInfo.specs)}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <!-- 使用图片预览组件 -->
          <GoodsCommentImage v-if="item.pictures.length" :pictures="item.pictures" />
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页组价 -->
     <XtxPagination @change-page='changePage' :page='1' :total='100' />
  </div>
</template>
<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { inject, ref, reactive, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
import XtxPagination from '@/components/library/xtx-pagination.vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage, XtxPagination },
  setup () {
    const detail = inject('goods')
    const conditions = ref(null)

    // A1------------------------------获取评论详情数据
    findCommentInfoByGoods(detail.id).then(ret => {
      ret.result.tags.unshift({
        type: 'img',
        title: '有图',
        tagCount: ret.result.hasPictureCount
      })
      ret.result.tags.unshift({
        type: 'all',
        title: '全部评价',
        tagCount: ret.result.evaluateCount
      })

      conditions.value = ret.result
    })

    // A2------------------------------点击有图-全部按钮
    const currentIndex = ref(0)
    const changeCondition = (index) => {
      currentIndex.value = index
      const info = conditions.value.tags[index]
      if (info.type === 'img') {
        // 证明选中有图按钮
        reqParams.hasPicture = true
        reqParams.tag = null
      } else if (info.type === 'all') {
        // 证明选中全部评论
        reqParams.hasPicture = null
        reqParams.sorField = null
      } else {
        // 证明选中其他条件
        reqParams.tag = info.title
        reqParams.hasPicture = null
      }
      reqParams.page = 1
    }

    // A3------------------------------点击最新-最热按钮
    const list = ref([])
    const total = ref(0)
    const reqParams = reactive({
      // 当前页码
      page: 1,
      // 每页的条数
      pageSize: 10,
      // 是否有图片
      hasPicture: null,
      // 筛选条件
      tag: null,
      // 排序的字段: null/praiseCount/createTime
      sortField: null
    })
    // A3-1 处理排序的切换
    const changeSort = (type) => {
      reqParams.sortField = type
      reqParams.page = 1
    }

    // A4------------------------------获取评论列表数据
    // 侦听参数的变化
    watch(reqParams, () => {
      findCommentListByGoods(detail.value.id, reqParams).then(ret => {
        // 获取总数
        total.value = ret.result.counts
        // 获取评论列表
        list.value = ret.result.items
      })
    }, {
      immediate: true
    })

    // 格式化昵称
    const formatNickName = (nickname) => {
      return nickname.substr(0, 1) + '****' + nickname.substr(-1)
    }
    // 格式化规格数据
    const formatSpec = (specs) => {
      return specs.reduce((result, tag) => result + tag.name + ':' + tag.nameValue + ' ', '')
    }

    // 监听页码的切换操作
    const changePage = (page) => {
      reqParams.page = page
    }

    return { conditions, currentIndex, changeCondition, list, formatNickName, formatSpec, changeSort, reqParams, changePage }
  }

}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor,50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,&:hover {
        color: @xtxColor;
      }
    }
  }
    .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
